package cn.commander.exception;

import cn.commander.constant.MvcResponseConstant;
import cn.commander.model.response.ResponseData;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import java.net.UnknownHostException;


/**
 * ClassName: GlobalExceptionHandler <br/>
 * Author: zhongyi_yang <br/>
 * Description:  全局异常类处理<br/>
 * Version: 1.0 <br/>
 * Date: 2018/5/28 11:34 <br/>
 * To change this template use File | Settings | File Templates.
 */
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

//	@ExceptionHandler(value = CustomException.class)
//	public Object handler(CustomException e) {
//		return getResponseData().buildResponseData(MvcResponseConstant.CODE_99999, null, e.getMessage());
//	}

	@ExceptionHandler(value = Exception.class)
	public Object handler(Exception e) {
		if (e.getCause() instanceof TooManyResultsException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_90013, null, e.getMessage());
		}
		if (e.getCause() instanceof UnknownHostException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_91001, null, e.getMessage());
		}
		if (e.getCause() instanceof CannotCreateTransactionException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_91000, null, e.getMessage());
		}
		if (e.getCause() instanceof PersistenceException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_91000, null, e.getMessage());
		}
		if (e.getCause() instanceof AccessDeniedException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_90001, null, e.getMessage());
		}
		if (e.getCause() instanceof NullPointerException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_99002, null, e.getMessage());
		}
		if (e.getCause() instanceof MessagingException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_99001, "邮件发送失败", e.getMessage());
		}
		if (e.getCause() instanceof UnauthorizedException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_90001, null, e.getMessage());
		}
		if (e.getCause() instanceof UnknownAccountException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_60007, null, e.getMessage());
		}
		if (e.getCause() instanceof IncorrectCredentialsException) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_60008, null, e.getMessage());
		}
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_99000, null, e.getMessage());
	}

	protected ResponseData getResponseData() {
		return new ResponseData();
	}

}
